package mx.com.mindbits.bandpay;

import mx.com.mindbits.bandpay.dao.BandPayDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.GPSTracker;
import mx.com.mindbits.bandpay.utils.GenericAlert;
import mx.com.mindbits.bandpay.utils.PropertiesBP;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.bandpay.vo.TAECompraVO;
import mx.com.mindbits.bridge.Blowfish;
import mx.com.mindbits.dao.transactions;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;



public class TransactionDetailActivity extends SherlockFragmentActivity {
	
	private Long idRx = (long) 0;
	private TAECompraVO compraTAE;
	private transactions _transaction;
	private BandPayDAO _bAndPay;
	private GPSTracker gps;
	private PropertiesBP properties;
	private ActionBar actionBar;
	private Menu menu;
	private MenuItem itemReprocesar;
	private Button btReprocesar;
	
	public Long getIdRx() {
		return idRx;
	}

	public void setIdRx(Long idRx) {
		this.idRx = idRx;
	}
	
	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		compraTAE = new TAECompraVO();
		_bAndPay = new BandPayDAO(this);

		gps = new GPSTracker(this);

		try{
			properties = new PropertiesBP(getApplicationContext());
			properties.loadProperties();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		////////////action bar settings///////////
		setSettingActionBar();		
		Bundle extras = getIntent().getExtras();

		
		if(extras != null)
			setIdRx(extras.getLong("idTransaction"));
		
		_transaction = _bAndPay.getTransactionById(getIdRx());
		
		setContentView(R.layout.activity_transaction_detail);
		
		Point size = new Point();
		DisplayMetrics displayM = this.getResources().getDisplayMetrics();
		size.y = displayM.heightPixels;
				
		Log.i("MAPA", "Y: " + size.y + " y m" +	"" +displayM.heightPixels);
		//findViewById(R.id.fragment_trans_map).getLayoutParams().height = (int) (((float) size.y) * 0.6);
		//findViewById(R.id.fragment_trans_detail).getLayoutParams().height = (int) (((float) size.y) * 0.4);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getSupportMenuInflater().inflate(R.menu.activity_transaction_detail, menu);
		setMenu( menu );
		itemReprocesar = (MenuItem) menu.findItem(R.id.menu_resend);
		btReprocesar = (Button) itemReprocesar.getActionView().findViewById(R.id.buttonGeneric);
		btReprocesar.setText(R.string.menu_tae_resend);
		btReprocesar.setBackgroundResource(R.drawable.blue_button_shape);
		btReprocesar.setEnabled(true);
		btReprocesar.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				setResend();
			}
			
			
		});
		
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case android.R.id.home:
			Intent intent =  new Intent(this,MainMenuActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		default:
			break;				
		}		
		return true;
	}
	
	public void setResend(){
		_transaction.getProducto();
		
		if(_transaction.getIsTae()){
			try{
				compraTAE.setAfiliacion(Blowfish.decode(ConstantsValues.AFILIACION));//properties.loadProperties().getProperty("afiliacion"));
				compraTAE.setIduser(Blowfish.decode(ConstantsValues.ID_USER));//properties.loadProperties().getProperty("id_user"));
				compraTAE.setLogin(Blowfish.decode(ConstantsValues.LOGIN));//properties.loadProperties().getProperty("login"));
				compraTAE.setUserpass(Blowfish.decode(ConstantsValues.USER));//properties.loadProperties().getProperty("user_password"));
			}catch(Exception e){
				Log.i(ConstantsValues.TAG_READER, "Error" + e);
			}
			compraTAE.setMonto(Double.toString(_transaction.getMonto()));
			Log.i(ConstantsValues.TAG_READER, "Resend <<<<<<<<<<<<<<<<<< " +this._transaction.getProducto()  );
			compraTAE.setProveedor(Integer.toString(this._transaction.getProveedor()));
			compraTAE.setNumbertae(Long.toString(_transaction.getNumeroTae()));
			compraTAE.setProducto(String.valueOf(_transaction.getProducto()));
			
			if(gps.getIsNetworkEnabled() || gps.getLatitude() != 0){
				
				compraTAE.setCx(String.valueOf(gps.getLatitude()));
				compraTAE.setCy(String.valueOf(gps.getLongitude()));
				//Comision no es un miembro del objeto TAEcompra
				
				Bundle bundle = new Bundle();
				bundle.putSerializable("TAEtrans", compraTAE);
				Intent intent = new Intent(this, CardDataCaptureActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}else{
				gps.showDialogSettingGPS();
			}
		}else{
			GenericAlert.generateAlertDialog(getApplicationContext().getString(R.string.app_name),
					getApplicationContext().getString(R.string.msn_reprocesamiento), this).show();
		}
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		gps.getLocation();
		
	}
	
	public void setSettingActionBar(){
		actionBar = getSupportActionBar();
		actionBar.setTitle("");
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
	}
}
